package com.ruoyi.examine.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;
import com.ruoyi.examine.entity.ExamineVo;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.place.domain.XlkjPlace;
import com.ruoyi.place.service.IXlkjPlaceService;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 审批
 */

@RestController
@RequestMapping("/app/examine")
public class ExamineControllerApp extends BaseController {

    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IXlkjPopulationService populationService;
    @Autowired
    private IXlkjHouseService houseService;
    @Autowired
    private IXlkjCompanyService companyService;
    @Autowired
    private IXlkjPlaceService placeService;
    @Autowired
    private IXlkjIniService iniService;

    @GetMapping("/list/{deptId}")
    public AjaxResult list(@PathVariable("deptId") String deptId){
        ExamineVo examineVo = new ExamineVo();

        List<SysDept> list = sysDeptService.selectDeptByParentId(Long.valueOf(deptId));
        String str = "(";
        for(int i =0;i<list.size();i++){
            str += list.get(i).getDeptId();
            if(i != list.size()-1){
                str += ",";
            }
        }
        str += ")";

        if(str.equals("()")){
//            examineVo.setOtherNum(0);
//            examineVo.setSecurityNum(0);
//            examineVo.setJurisdictionNum(0);
//            examineVo.setPlaceNum(0);
//            examineVo.setCompanyNum(0);
//            examineVo.setHouseNum(0);
//            examineVo.setPopulationNum(0);
//            examineVo.setIncidentNum(0);
//            return AjaxResult.success(examineVo);
            str = null;
        }

        XlkjPopulation population = new XlkjPopulation();
        population.setStatus("1");
        population.setDepartmentList(str);
        List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(population);
        examineVo.setPopulationNum(xlkjPopulations.size());

        XlkjHouse xlkjHouse = new XlkjHouse();
        xlkjHouse.setStatus("1");
        xlkjHouse.setDepartmentList(str);
        List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(xlkjHouse);
        examineVo.setHouseNum(xlkjHouses.size());

        XlkjCompany company = new XlkjCompany();
        company.setStatus("1");
        company.setDepartmentList(str);
        List<XlkjCompany> xlkjCompanies = companyService.selectXlkjCompanyList(company);
        examineVo.setCompanyNum(xlkjCompanies.size());

        XlkjPlace place = new XlkjPlace();
        place.setStatus("1");
        place.setDepartmentList(str);
        List<XlkjPlace> xlkjPlaces = placeService.selectXlkjPlaceList(place);
        examineVo.setPlaceNum(xlkjPlaces.size());


        XlkjIni ini = new XlkjIni();
//        ini.setStatus("1");
        ini.setDepartmentList(str);
        ini.setType("1");
        List<XlkjIni> xlkjIncidents = iniService.selectXlkjIniList(ini);

        ini.setType("2");
        List<XlkjIni> xlkjIncidents1 = iniService.selectXlkjIniList(ini);
        ini.setType("3");
        List<XlkjIni> xlkjIncidents2 = iniService.selectXlkjIniList(ini);

        ini.setType("4");
        List<XlkjIni> xlkjIncidents3 = iniService.selectXlkjIniList(ini);

        ini.setStatus("6");
        ini.setType("1");
        List<XlkjIni> xlkjIncidents4 = iniService.selectXlkjIniList(ini);
        examineVo.setJurisdictionNum(xlkjIncidents.size() + xlkjIncidents4.size());

        ini.setType("2");
        List<XlkjIni> xlkjIncidents5 = iniService.selectXlkjIniList(ini);
        ini.setType("3");
        List<XlkjIni> xlkjIncidents6 = iniService.selectXlkjIniList(ini);
        examineVo.setSecurityNum(xlkjIncidents1.size() + xlkjIncidents2.size() + xlkjIncidents5.size() + xlkjIncidents6.size());

        ini.setType("4");
        List<XlkjIni> xlkjIncidents7 = iniService.selectXlkjIniList(ini);
        examineVo.setOtherNum(xlkjIncidents3.size() + xlkjIncidents7.size());

        return AjaxResult.success(examineVo);
    }

}
